package com.starleague.battletech.unit.mech.jpa.appengine;

import com.starleague.battletech.BattletechException;
import com.starleague.battletech.unit.BattletechUnitEngineType;
import com.starleague.battletech.unit.IWeight;
import com.starleague.battletech.unit.mech.AbstractMechEngineFactory;
import com.starleague.battletech.unit.mech.IMutableMech;
import com.starleague.battletech.unit.mech.MechHitLocationPosition;

public class MechEngineGaeFactory
        extends AbstractMechEngineFactory
{
    // TODO: Make not hard-coded
    private static final int[] CENTER_TORSO_SLOT_INDICES = { 0, 1, 2, 7, 8, 9 };

    @Override
    protected MechEngineGae addEngine(IMutableMech mech,
                                      IWeight engineWeight,
                                      BattletechUnitEngineType engineType,
                                      int engineRating) throws BattletechException
    {
        MechEngineGae engine = new MechEngineGae((MechGae) mech, engineWeight, engineType, engineRating);

        MechCriticalSlotGae[] centerTorsoCriticalSlots = ((MechGae) mech).getCriticalSlotsFor(MechHitLocationPosition.CENTER_TORSO);
        for (int requiredCenterTorsoSlotIndex : CENTER_TORSO_SLOT_INDICES)
        {
            if (centerTorsoCriticalSlots[requiredCenterTorsoSlotIndex] != null)
                throw new BattletechException("CenterTorso should have a free slot at ["
                                              + requiredCenterTorsoSlotIndex
                                              + "]: "
                                              + centerTorsoCriticalSlots[requiredCenterTorsoSlotIndex]);

            engine.getCriticalSlots().add(new MechEngineCriticalSlotGae(engine,
                                                                        MechHitLocationPosition.CENTER_TORSO,
                                                                        requiredCenterTorsoSlotIndex));
        }

        MechCriticalSlotGae[] leftTorsoCriticalSlots = ((MechGae) mech).getCriticalSlotsFor(MechHitLocationPosition.LEFT_TORSO);
        MechCriticalSlotGae[] rightTorsoCriticalSlots = ((MechGae) mech).getCriticalSlotsFor(MechHitLocationPosition.RIGHT_TORSO);
        for (int requiredSideTorsoSlotIndex = 0; requiredSideTorsoSlotIndex < getNumRequiredSideTorsoSlots(engineType); ++requiredSideTorsoSlotIndex)
        {
            if (leftTorsoCriticalSlots[requiredSideTorsoSlotIndex] != null)
                throw new BattletechException("LeftTorso should have a free slot at ["
                                              + requiredSideTorsoSlotIndex
                                              + "]: "
                                              + leftTorsoCriticalSlots[requiredSideTorsoSlotIndex]);

            engine.getCriticalSlots().add(new MechEngineCriticalSlotGae(engine,
                                                                        MechHitLocationPosition.LEFT_TORSO,
                                                                        requiredSideTorsoSlotIndex));

            if (rightTorsoCriticalSlots[requiredSideTorsoSlotIndex] != null)
                throw new BattletechException("RightTorso should have a free slot at ["
                                              + requiredSideTorsoSlotIndex
                                              + "]: "
                                              + rightTorsoCriticalSlots[requiredSideTorsoSlotIndex]);

            engine.getCriticalSlots().add(new MechEngineCriticalSlotGae(engine,
                                                                        MechHitLocationPosition.RIGHT_TORSO,
                                                                        requiredSideTorsoSlotIndex));
        }

        ((MechGae) mech).setEngine(engine);
        return engine;
    }
}
